﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;

namespace DG.Common.Settings
{
    public class AppRegistryConfig<T> : AppRegistryConfig
    {
        public AppRegistryConfig(string folderPath)
            : base(folderPath)
        {
        }

        public string this[T key]
        {
            get { return this["", key.ToString()]; }
            set { this["", key.ToString()] = value; }
        }

        public bool Exists(T key)
        {
            return Exists("", key.ToString());
        }
    }
    public class AppRegistryConfig  
    {
        private string _folderPath;
        public string this[string key]
        {
            get { return this["", key]; }
            set { this["", key] = value; }
        }

        public string this[string subDirectory, string key]
        {
            get
            {
                var registryKey = GetKey(subDirectory);
                var ret = registryKey.GetValue(key);
                registryKey.Close();
                return ret.ToString();
            }
            set
            {
                var registryKey = GetKey(subDirectory);
                registryKey.SetValue(key, value);
                registryKey.Close();
            }
        }

        public string FolderPath
        {
            get { return _folderPath; }
            set { _folderPath = value; }
        }

        public bool Exists(string key)
        {
            return Exists("", key);
        }
        public bool Exists(string subDirectory, string key)
        {
            var registryKey = GetKey(subDirectory);
            return !ReferenceEquals(registryKey.GetValue(key, DBNull.Value), DBNull.Value);
        }
        public void DeleteSubKey(string subKey)
        {
            var key = GetKey();
            key.DeleteSubKeyTree(subKey);
        }
        public void DeleteKeyValue(string valueName)
        {
            DeleteKeyValue("", valueName);
        }
        public void DeleteKeyValue(string subKey, string valueName)
        {
            var key = GetKey(subKey);
            key.DeleteValue(valueName);
        }
        public IEnumerable<string> GetSubKeyNames(string path)
        {
            return GetKey(path).GetSubKeyNames();
        }
        public IEnumerable<string> GetValueNames(string path)
        {
            return GetKey(path).GetValueNames();
        }
        public IEnumerable<KeyValuePair<string, string>> GetKeyValueNames(string path)
        {
            var key = GetKey(path);
            return key.GetValueNames().Select(value => new KeyValuePair<string, string>(value, key.GetValue(value).ToString())).ToList();
        }
        private RegistryKey GetKey()
        {
            return  Registry.CurrentUser.CreateSubKey("Software\\" + _folderPath);
        }
        private RegistryKey GetKey(string path)
        {
            return string.IsNullOrEmpty(path) ? GetKey() : GetKey().CreateSubKey(path);
        }

        public AppRegistryConfig(string folderPath)
        {
            _folderPath = folderPath;
        }

    }
}